<?php

class Cabang extends Controller{
/**
	 * Constructor
	 */
function Cabang()
{
parent::Controller();
$this->load->model('Cabang_model','',TRUE);
$this->load->library('table');

}

//inisialisasi variable untuk $title(id elemen body)*/

var $title = 'Daftar Cabang';


//periksa user state jika login akan menampilkan halaman cabang, jika tidak redirect login*/

function index()
{
if($this->session->userdata('login') == TRUE)
{
if($this->session->userdata('id_usergroup')== 6||$this->session->userdata('id_usergroup') == 1 ||$this->session->userdata('id_usergroup') == 2 )
{
if($this->session->userdata('analisa') == TRUE){
$this->analisa_model->ana_off();
$this->get_all();
}
else{
$this->get_all();
}
}
else
{
redirect('home');
}
}
else
{
redirect('login');
}
}

//menampilkan semua data cabang*/

function get_all()
{

$data['title']=$this->title;
$data['h2_title']='Cabang';
$data['main']='cabang/cabang';

//load data*/

$query = $this->Cabang_model->get_all();
$cabang = $query->result();
$num_rows = $query->num_rows();

if($num_rows > 0)
{
//table template*/
$tmpl = array('table_open' => '<table border="1" cellpadding="1" cellspacing="1">', 'row_alt_start'  => '<tr class="">',
							'row_alt_end'    => '</tr>'
						  );
$this->table->set_template($tmpl);

//table heading*/
$this->table->set_empty("&nbsp;");
$this->table->set_heading('No','Cabang','Kota','Actions');
$i=0;

foreach($cabang as $row)
{
$this->table->add_row(++$i, $row->cabang, $row->kota,
													anchor('cabang/view/'.$row->id_cabang,'view',array('class' => 'view')).' '.
										anchor('cabang/update/'.$row->id_cabang,'update',array('class' => 'update')).' '.
										anchor('cabang/delete/'.$row->id_cabang,'hapus',array('class'=> 'delete','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')"))
										);
													
}
$data['table']=$this->table->generate();
}
else
{
$data['message']='Tidak ditemukan satupun data cabang!';
}
$data['link']=array('link_add'=>anchor('cabang/add/','tambah data',array('class'=>'add')));

//load view*/
$this->load->view('base',$data);
	}												
//hapus cabang*/

function delete($id_cabang)
{
$this->Cabang_model->delete($id_cabang);
$this->session->set_flashdata('message','satu data cabang berhasil dihapus');
redirect('cabang');
}
//halaman tambah cabang*/

function add()
{
$data['title'] = $this->title;
$data['h2_title'] = 'Tambah Cabang Koperasi';
$data['main'] = 'cabang/cabang_form';
$data['form_action'] = site_url('cabang/add_process');
$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back')));

$this->load->view('base',$data);
}
//proses tambah data cabang*/

function add_process()
{

$data['title'] = $this->title;
$data['h2_title'] = 'Tambah Cabang Koperasi';
$data['main'] = 'cabang/cabang_form';
$data['form+action'] = site_url('cabang/add_process');
$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);	
										
//set rule untuk validasi*/

$this->form_validation->set_rules('cabang', 'Cabang', 'required|max_length[200]');
		$this->form_validation->set_rules('alamat', 'alamat', 'required|max_length[200]');
		$this->form_validation->set_rules('kota', 'kota', 'required|max_length[50]');
		$this->form_validation->set_rules('phone', 'phone', 'required|max_length[50]');
		$this->form_validation->set_rules('fax', 'fax', 'required|max_length[50]');
		$this->form_validation->set_rules('email', 'email', 'required|max_length[50]');
		
//jika sukses*/
if($this->form_validation->run()== TRUE)
{
	$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
						);
			  
//menyimpan data di tabel cabang*/
$this->Cabang_model->add($cabang);

$this->session->set_flashdata('message','Satu data cabang berhasil dismpan!');	
redirect('cabang');
}

//jika validasi gagal*/

else
{
$this->load->view('base',$data);
}
}
//halaman view cabang*/
		  
function view($id_cabang)
{
$data['title'] = $this->title;
//$data['h2_title'] = 'Detail Cabang';*/
$data['main'] = 'cabang/cabang_view';
$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back')));

//cari data di database*/
$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang);

//membuat session untuk penyimpanan data PK(idcabang)*/
$this->session->set_userdata('id_cabang', $cabang->id_cabang);

//data untuk mengisi field form*/
$data['default']['cabang'] = $cabang->cabang;
$data['default']['alamat'] = $cabang->alamat;
$data['default']['kota'] = $cabang->kota;
$data['default']['phone'] = $cabang->phone;
$data['default']['fax'] = $cabang->fax;
$data['default']['email'] = $cabang->email;

$this->load->view('base', $data);
}
//pindah ke halaman update cabang*/

function update($id_cabang)
{
$data['title'] = $this->title;
$data['h2_title'] = 'Update Data Koperasi Cabang';
$data['main'] = 'cabang/cabang_form';
$data['form_action'] = site_url('cabang/update_process');
$data['link'] = array('link_back' => anchor('cabang','kembali', array('class' => 'back')));
		

//cari data di database*/
$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang);

//membuat session untuk penyimpanan data PK(idcabang)*/
$this->session->set_userdata('id_cabang',$cabang->id_cabang);

//data untuk mengisi field form*/
$data['default']['cabang'] = $cabang->cabang;
$data['default']['alamat'] = $cabang->alamat;
$data['default']['kota'] = $cabang->kota;
$data['default']['phone'] = $cabang->phone;
$data['default']['fax'] = $cabang->fax;
$data['default']['email'] = $cabang->email;

$this->load->view('base',$data);		
}
//proses update cabang*/

function update_process()
{

$data['title'] = $this->title;
$data['h2_title'] = 'Update Data Koperasi Cabang';
$data['main'] = 'cabang/cabang_form';
$data['form_action'] = site_url('cabang/update_process');
$data['link'] 	  = array('link_back' => anchor('cabang','kembali', array('class' => 'back')));

//set rule untuk validasi*/

$this->form_validation->set_rules('cabang', 'Cabang', 'required|max_length[200]');
		$this->form_validation->set_rules('alamat', 'alamat', 'required|max_length[200]');
		$this->form_validation->set_rules('kota', 'kota', 'required|max_length[50]');
		$this->form_validation->set_rules('phone', 'phone', 'required|max_length[50]');
		$this->form_validation->set_rules('fax', 'fax', 'required|max_length[50]');
		$this->form_validation->set_rules('email', 'email', 'required|max_length[50]');
		
//jika sukses*/
if($this->form_validation->run()== TRUE)
{
	$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
						);
			  
//mengupdate data di tabel cabang*/
$this->Cabang_model->update($this->session->userdata('id_cabang'), $cabang);

$this->session->set_flashdata('message','Satu data cabang berhasil diupdate!');	
redirect('cabang');
}

//jika validasi gagal*/

else
{
$this->load->view('base',$data);

}
}
}


// END Cabang_model Class

/* End of file cabang_model.php */
/* Location: ./system/application/controllers/cabang.php */